<?php

/**
 * 行业数据提取
 *
 * @author haifeng.wu
 */

require_once 'CommonController.php';
class Admin_IndustryDataController extends Admin_CommonController
{
    /**
     * 初始化函数
     * 
     * @return void
     */
    public function init()
    {
        parent::init();
        $this->Model = new Application_Model_DayData();
        $this->view->title = "管理中心";
    }

    /**
     * 默认动作
     * 
     * @return void
     */
    public function indexAction()
    {
        $vars = array();
        $wheres = array();
        $vars['year'] = $this->_request->getPost('year');
        $vars['month'] = $this->_request->getPost('month');
        $vars['endyear'] = $this->_request->getPost('endyear');
        $vars['endmonth'] = $this->_request->getPost('endmonth');
        $this->view->vars = $vars;
        $productArr = $this->_request->getPost('product');
        $product = !empty($productArr) ? implode("','",$productArr) : '';
        $osnameArr = $this->_request->getPost('osname');
        $osname = !empty($osnameArr) ? implode("','",$osnameArr) : '';
        if ($this->_request->isPost()) {
            if (empty($vars['year']) && empty($vars['month']) && empty($vars['endyear']) && empty($vars['endmonth'])) {
                echo $this->view->message('请正确选择时间') ;
                exit;
            }
            $startDate = $vars['year'] . '-' . $vars['month'] . '-1';
            $endDate = $vars['endyear'] . '-' . $vars['endmonth'] . '-30';
            if(strtotime($endDate) < strtotime($startDate)){
                echo $this->view->message('请正确选择时间') ;
                exit;
            }
            $sql = "SELECT  a.productCode,a.date,a.sourceId,b.sourceName,a.osName,sum(a.installNum) as installNum,sum(a.okOrderNum) as okOrderNum,sum(a.okOrderAmount) as okOrderAmount,sum(a.okOrderClientNum) as okOrderClientNum,sum(a.bootNum) as bootNum,sum(a.bootClientNum) as bootClientNum   
                    FROM  mob_day_data  AS a 
                    LEFT JOIN mob_source AS b 
                    ON (a.sourceId = b.sourceId AND a.sourceSubId = b.sourceSubId AND a.productCode = b.productCode)
                    WHERE a.`date`>='{$startDate}'  AND a.`date`<='{$endDate}' 
                    AND a.`productCode` in ('{$product}') 
                    AND a.`osName` in ('{$osname}')
                    GROUP BY a.productCode,a.sourceId,a.osName,a.date,b.sourceName";
            $rowTmp = $this->Model->getAdapter()->fetchAll($sql);
            $row = array();
            $res = array();
            foreach ($rowTmp as $item) {
                $mon = substr($item['date'], 0, 7);
                $str = 'other';
                if (stristr($item['sourceName'],"苹果")) {
                    $str = 'apple';
                } else if (stristr($item['sourceName'],"91")) {
                    $str = 'nineOne';
                }
                $item['osName'] = strtolower($item['osName']);
                $res[$mon][$item['productCode']][$item['osName']][$str]['installNum'] = isset($res[$mon][$item['productCode']][$item['osName']][$str]['installNum']) ? ($res[$mon][$item['productCode']][$item['osName']][$str]['installNum']+$item['installNum']) : $item['installNum'];
                $res[$mon][$item['productCode']][$item['osName']][$str]['okOrderNum'] = isset($res[$mon][$item['productCode']][$item['osName']][$str]['okOrderNum']) ? ($res[$mon][$item['productCode']][$item['osName']][$str]['okOrderNum']+$item['okOrderNum']) : $item['okOrderNum'];
                $res[$mon][$item['productCode']][$item['osName']][$str]['okOrderAmount'] = isset($res[$mon][$item['productCode']][$item['osName']][$str]['okOrderAmount']) ? ($res[$mon][$item['productCode']][$item['osName']][$str]['okOrderAmount']+$item['okOrderAmount']) : $item['okOrderAmount'];
                $res[$mon][$item['productCode']][$item['osName']][$str]['okOrderClientNum'] = isset($res[$mon][$item['productCode']][$item['osName']][$str]['okOrderClientNum']) ? ($res[$mon][$item['productCode']][$item['osName']][$str]['okOrderClientNum']+$item['okOrderClientNum']) : $item['okOrderClientNum'];
                $res[$mon][$item['productCode']][$item['osName']][$str]['bootNum'] = isset($res[$mon][$item['productCode']][$item['osName']][$str]['bootNum']) ? ($res[$mon][$item['productCode']][$item['osName']][$str]['bootNum']+$item['bootNum']) : $item['bootNum'];
                $res[$mon][$item['productCode']][$item['osName']][$str]['bootClientNum'] = isset($res[$mon][$item['productCode']][$item['osName']][$str]['bootClientNum']) ? ($res[$mon][$item['productCode']][$item['osName']][$str]['bootClientNum']+$item['bootClientNum']) : $item['bootClientNum'];
                
            }
            
            foreach ($res as $d => $rese) {
                foreach ($rese as $key => $val) {
                 foreach ($val as $k => $v) {
                      foreach ($v as $it => $ids) {
                          $ids['date'] = $d;
                          $ids['osName'] = $k;
                          $ids['sourceType'] = $it;
                          $ids['productCode'] = $key;
                          $row[] = $ids;
                       }
                  }
                }
            }
            foreach ($row as $key => $item) {
                if ($item['productCode'] == 'vjia' || $item['productCode'] == 'ibanggo' || $item['productCode'] == 'mh' || $item['productCode'] == 'letao' || $item['productCode'] == 'vancl' || $item['osName'] == '' || $item['installNum'] == 0 || $item['bootNum'] == 0) {
                    unset($row[$key]);
                }
            }
            $partnerProduct = new Application_Model_PartnerProduct();
            $partnerProductArr =  $partnerProduct->fetchAll();
            $parr = array();
            foreach ($partnerProductArr as $item) {
                $parr[$item['code']] = $item['name'];
            }
            $sourceArr = array('other'=>'其他','apple'=>'appstore','nineOne'=>'91手机助手');
            $resArray = array();
            $exportNum =1;
            foreach ($row as $item) {
                $exportNum++;
                $resArray['A'.$exportNum]= isset($parr[$item['productCode']]) ? $parr[$item['productCode']] : $item['productCode'];
                $resArray['B'.$exportNum]= $item['osName'];
                $resArray['C'.$exportNum]= $item['date'];
                $resArray['D'.$exportNum]= $sourceArr[$item['sourceType']];
                $resArray['E'.$exportNum]= $item['installNum'];
                $resArray['F'.$exportNum]= $item['okOrderNum'];
                $resArray['G'.$exportNum]= $item['okOrderAmount'];
                $resArray['H'.$exportNum]= $item['okOrderClientNum'];
                $resArray['I'.$exportNum]= $item['bootNum'];
                $resArray['J'.$exportNum]= $item['bootClientNum'];
            }
            $fileName = ($vars['year'] == $vars['endyear'] && $vars['month'] == $vars['endmonth']) ? $vars['year'].'年'.$vars['month'].'月' : $vars['year'].'年'.$vars['month'].'月到'.$vars['endyear'].'年'.$vars['endmonth'].'月';
            $table = array();
            $table[0]['tableName'] = $vars['year'].'年'.$vars['month'].'行业数据';
            $table[0]['title'] = array('项目','系统','月份','渠道来源','激活量','订单量','订单金额','购买人次','启动次数','启动人次');
            $table[0]['con'] = $resArray;
            $fileName = $fileName."行业数据";
            $fontArr = array();
            foreach ($row as $key => $item) {
                $fontArr[$key]['productCode']= isset($parr[$item['productCode']]) ? $parr[$item['productCode']] : $item['productCode'];
                $fontArr[$key]['osName']= $item['osName'];
                $fontArr[$key]['date']= $item['date'];
                $fontArr[$key]['sourceType']= $sourceArr[$item['sourceType']];
                $fontArr[$key]['installNum']= $item['installNum'];
                $fontArr[$key]['okOrderNum']= $item['okOrderNum'];
                $fontArr[$key]['okOrderAmount']= $item['okOrderAmount'];
                $fontArr[$key]['okOrderClientNum']= $item['okOrderClientNum'];
                $fontArr[$key]['bootNum']= $item['bootNum'];
                $fontArr[$key]['bootClientNum']= $item['bootClientNum'];
            }
            $countarr = array();
            $firarr = array();
            
            
            foreach ($fontArr as $item) {

                $countarr[$item['date']][$item['sourceType']]['installNum'] = isset($countarr[$item['date']][$item['sourceType']]['installNum']) ? $countarr[$item['date']][$item['sourceType']]['installNum']+$item['installNum'] : $item['installNum'];
                $countarr[$item['date']][$item['sourceType']]['okOrderNum'] = isset($countarr[$item['date']][$item['sourceType']]['okOrderNum']) ? $countarr[$item['date']][$item['sourceType']]['okOrderNum']+$item['okOrderNum'] : $item['okOrderNum'];
                $countarr[$item['date']][$item['sourceType']]['okOrderAmount'] = isset($countarr[$item['date']][$item['sourceType']]['okOrderAmount']) ? $countarr[$item['date']][$item['sourceType']]['okOrderAmount']+$item['okOrderAmount'] : $item['okOrderAmount'];
                $countarr[$item['date']][$item['sourceType']]['okOrderClientNum'] = isset($countarr[$item['date']][$item['sourceType']]['okOrderClientNum']) ? $countarr[$item['date']][$item['sourceType']]['okOrderClientNum']+$item['okOrderClientNum'] : $item['okOrderClientNum'];
                $countarr[$item['date']][$item['sourceType']]['bootNum'] = isset($countarr[$item['date']][$item['sourceType']]['bootNum']) ? $countarr[$item['date']][$item['sourceType']]['bootNum']+$item['bootNum'] : $item['bootNum'];
                $countarr[$item['date']][$item['sourceType']]['bootClientNum'] = isset($countarr[$item['date']][$item['sourceType']]['bootClientNum']) ? $countarr[$item['date']][$item['sourceType']]['bootClientNum']+$item['bootClientNum'] : $item['bootClientNum'];
                
                
                $firarr[$item['date']]['installNum'] = isset($firarr[$item['date']]['installNum']) ? $firarr[$item['date']]['installNum']+$item['installNum'] : $item['installNum'];
                $firarr[$item['date']]['okOrderNum'] = isset($firarr[$item['date']]['okOrderNum']) ? $firarr[$item['date']]['okOrderNum']+$item['okOrderNum'] : $item['okOrderNum'];
                $firarr[$item['date']]['okOrderAmount'] = isset($firarr[$item['date']]['okOrderAmount']) ? $firarr[$item['date']]['okOrderAmount']+$item['okOrderAmount'] : $item['okOrderAmount'];
                $firarr[$item['date']]['okOrderClientNum'] = isset($firarr[$item['date']]['okOrderClientNum']) ? $firarr[$item['date']]['okOrderClientNum']+$item['okOrderClientNum'] : $item['okOrderClientNum'];
                $firarr[$item['date']]['bootNum'] = isset($firarr[$item['date']]['bootNum']) ? $firarr[$item['date']]['bootNum']+$item['bootNum'] : $item['bootNum'];
                $firarr[$item['date']]['bootClientNum'] = isset($firarr[$item['date']]['bootClientNum']) ? $firarr[$item['date']]['bootClientNum']+$item['bootClientNum'] : $item['bootClientNum'];
            }
            
            $tmplateCountarr = array();
            foreach ($countarr as $key=>$v) {
                foreach ($v as $k=>$item) {
                    $tmplateCountarr[$key][$k]['onepOrder'] = round($item['okOrderAmount']/$item['okOrderNum']);
                    $tmplateCountarr[$key][$k]['bootOnper'] = round(($item['okOrderClientNum']/$item['bootClientNum'])*100,2);
                    $tmplateCountarr[$key][$k]['onepBoot'] = round($item['bootNum']/$item['bootClientNum'],1);
                    $tmplateCountarr[$key][$k]['okOrderNum'] = $item['okOrderNum'];
                    $tmplateCountarr[$key][$k]['installNum'] = $item['installNum'];
                }

            }
            $tmplateFirarr = array();
            foreach ($firarr as $key=>$item) {
                $tmplateFirarr[$key]['onepOrder'] = round($item['okOrderAmount']/$item['okOrderNum']);
                $tmplateFirarr[$key]['bootOnper'] = round(($item['okOrderClientNum']/$item['bootClientNum'])*100,2);
                $tmplateFirarr[$key]['onepBoot'] = round($item['bootNum']/$item['bootClientNum'],1);
                $tmplateFirarr[$key]['okOrderNum'] = $item['okOrderNum'];
                $tmplateFirarr[$key]['installNum'] = $item['installNum'];
            }
            $this->view->table = serialize($table);
            $this->view->fileName = $fileName;
            $this->view->firarr = $tmplateFirarr;
            $this->view->countarr = $tmplateCountarr;
        }
        $partnerProductModel = new Application_Model_PartnerProduct();
        $sqlpa = "SELECT * FROM {$partnerProductModel->getTableName()}";
        $productArr = $partnerProductModel->getAdapter()->fetchAll($sqlpa);
        $this->view->productArr = $productArr;
    }
    
    public function exportAction()
    {
        if ($this->_request->isPost()) {
            $table = unserialize($this->_request->getPost('tableValue'));
            $fileName = $this->_request->getPost('fileName');
            $fName = iconv('UTF-8','GBK//IGNORE',"{$fileName}行业数据");
            $this->Model->excelExport($table,$fName);
        }
        exit;
    }
    
    
    
    
}